/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import modelo.AcaoBO;

/**
 *
 * @author rodrigo
 */
public class AcaoDAO extends Conexao {
    
    private ObjetivoDAO objetivoDAO;
    private ViewsDAO viewsDAO;
    
    public AcaoDAO() throws SQLException {
        super();
        objetivoDAO = new ObjetivoDAO();
        viewsDAO = new ViewsDAO();

    }
    
    public void inserirAcao(AcaoBO acaoBO) throws SQLException {
        this.setPreparedStatement("INSERT INTO acao (id_acao, nome, descricao, data_cadastro, status, id_objetivo) VALUES (nextval('Seq_acao'),?,?,?,?,?)");
        this.getPreparedStatement().setString(1, acaoBO.getNome());
        this.getPreparedStatement().setString(2, acaoBO.getDescricao());
        this.getPreparedStatement().setDate(3, new java.sql.Date(acaoBO.getData().getTime()));
        this.getPreparedStatement().setInt(4, acaoBO.getStatus());
        this.getPreparedStatement().setInt(5, acaoBO.getObjetivoBO().getIdObjetivo());
        this.getPreparedStatement().executeUpdate();
        
    }
    
    public void alterarAcao(AcaoBO acaoBO) throws SQLException {
        
        this.setPreparedStatement(" UPDATE acao SET nome = ?, descricao = ?, data_cadastro = ?, status = ?, id_objetivo = ? WHERE id_acao = ? ");
        this.getPreparedStatement().setString(1, acaoBO.getNome());
        this.getPreparedStatement().setString(2, acaoBO.getDescricao());
        this.getPreparedStatement().setDate(3, new java.sql.Date(acaoBO.getData().getTime()));
        this.getPreparedStatement().setInt(4, acaoBO.getStatus());
        this.getPreparedStatement().setInt(5, acaoBO.getObjetivoBO().getIdObjetivo());
        this.getPreparedStatement().setInt(6, acaoBO.getIdAcao());
        
        this.getPreparedStatement().executeUpdate();
        
    }
    
    public void excluirAcao(AcaoBO acaoBO) throws SQLException {
        this.setPreparedStatement(" DELETE FROM acao WHERE id_acao = ? ");
        this.getPreparedStatement().setInt(1, acaoBO.getIdAcao());
        this.getPreparedStatement().executeUpdate();
        
    }
    
    public List<AcaoBO> listarAcoes(String sql) throws SQLException {
        List<AcaoBO> acoes = new ArrayList<>();
        this.setPreparedStatement(sql);
        this.setResultSet(this.getPreparedStatement().executeQuery());
        while (this.getResultSet().next()) {
            AcaoBO acaoBO = new AcaoBO();
            acaoBO.setIdAcao(this.getResultSet().getInt("id_acao"));
            acaoBO.setNome(this.getResultSet().getString("nome"));
            acaoBO.setDescricao(this.getResultSet().getString("descricao"));
            acaoBO.setObjetivoBO(objetivoDAO.listarInstituicaoPorId(this.getResultSet().getInt("id_objetivo")));
            acaoBO.setData(this.getResultSet().getDate("data_cadastro"));
            acaoBO.setStatus(this.getResultSet().getInt("status"));
            acoes.add(acaoBO);
        }
        
        return acoes;
    }
    
    
    
}
